package com.gaojinqi.sys.core.aware;

import com.gaojinqi.sys.common.constant.UserConst;
import com.gaojinqi.sys.core.auth.util.LoginContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 用户名
 *
 * @author gaojinqi
 * @version 1.0
 * @since 2020年04月15日
 */
@Component
@Slf4j
public class UserNameAuditorAware implements AuditorAware<String> {

    @Override
    public Optional<String> getCurrentAuditor() {
        String userName;
        try {
            userName = LoginContextHolder.getContext().getUserName();
        } catch (Exception e) {
            log.debug("UserNameAuditorAware.getCurrentAuditor  msg = {}", e.getMessage());
            userName = UserConst.SYSTEM_USER_NAME;
        }
        //  获取用户名
        return Optional.of(userName);
    }

}
